﻿#include "MaterialDeleteDialog.h"
#include "ui_MaterialDeleteDialog.h"
#include"MaterialEditWidget.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/PropertyWidget.h"
#include "OperatorsInterface/ParaWidgetInterfaceOperator.h"

#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"

namespace GUI
{
    MaterialDeleteDialog::MaterialDeleteDialog(Interface::FITKAbstractMaterial * obj, EventOper::ParaWidgetInterfaceOperator * oper) :
        GUIDialogBase(FITKAPP->getGlobalData()->getMainWindow()), _oper(oper), _obj(obj)
    {
        _ui = new Ui::MaterialDeleteDialog();
        _ui->setupUi(this);

        if (_obj) {
            QString text = tr("Delete %1").arg(_obj->getDataObjectName());
            _ui->label_Name->setText(text);
        }

        setWindowTitle(tr("Material Delete"));
    }

    MaterialDeleteDialog::~MaterialDeleteDialog()
    {
        if (_ui)delete _ui;
    }

    void MaterialDeleteDialog::on_pushButton_OK_clicked()
    {
        if (_obj == nullptr)return;

        Interface::FITKMaterialManager* materialManager = FITKAPP->getGlobalData()->getMaterialData<Interface::FITKMaterialManager>();
        if (!materialManager)return;//获取失败返回nullpttr，因此要检查返回值是否为空，避免空指针


        //判断删除的数据是否是当前界面
        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return;

        //所要删除的几何对象ID
        int deleteObjId = _obj->getDataObjectID();

        //获取当前的几何界面与几何id
        GUI::MaterialWidgetBase* currentWidget = dynamic_cast<GUI::MaterialWidgetBase*>(propertyWidget->getCurrentWidget());
        if (currentWidget) {
//            //当前显示界面几何id
//            int CurrentWidgetGeoID = currentWidget->getCurrentGeoCommand()->getDataObjectID();
//            //如果删除的数据是当前界面,删除当前界面
//            if (CurrentWidgetGeoID == _obj->getDataObjectID()) {
//                propertyWidget->init();
//            }
        }

        //清除几何对象
        materialManager->removeDataByID(deleteObjId);
        _oper->execProfession();
        this->accept();
    }

    void MaterialDeleteDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }
}
